package model.cnf;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Represent a clause in a CNF.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class Clause {

	private List<Literal>	literals;

	/**
	 * Create a clause using a string representation of the clause.
	 * 
	 * @param line
	 *            string to use.
	 */
	public Clause(String line) {
		literals = new ArrayList<Literal>();

		int i = 0;
		Scanner sc = new Scanner(line);
		while ((i = sc.nextInt()) != 0)
			literals.add(new Literal(i));

		// cleanClause();
	}

	/**
	 * Create a clause using a list of {@link Literal}.
	 * 
	 * @param list
	 *            list to use.
	 */
	public Clause(List<Literal> list) {
		literals = new ArrayList<Literal>(list);
		// cleanClause();
	}

	@Override
	public String toString() {
		StringBuffer s = new StringBuffer();
		for (Literal l : literals)
		{
			s.append(l.toString());
			s.append(" ");
		}
		return s.toString();
	}

	/**
	 * Return the literal number "i".
	 * 
	 * @param i
	 *            number of the literal.
	 * @return the literal number "i".
	 */
	public Literal getLiteral(int i) {
		return literals.get(i);
	}

	/**
	 * 
	 * @return if the clause is unit.
	 */
	public boolean isUnit() {
		return (literals.size() == 1);
	}

	/**
	 * 
	 * @return if the clause is binary.
	 */
	public boolean isBinary() {
		return (literals.size() == 2);
	}

	/**
	 * 
	 * @return the number of literal in the clause.
	 */
	public int size() {
		return literals.size();
	}

}
